package org.hhh.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hhh.basic.util.AjaxResult;
import org.hhh.basic.util.PageList;
import org.hhh.org.domain.Systemdictionarytype;
import org.hhh.org.query.SystemdictionarytypeQuery;
import org.hhh.org.service.ISystemdictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典管理接口数据对接json格式
 */

@RestController
@RequestMapping("/sdt")
@Api(tags = "数据字典接口",description = "数据字典接口详细描述")
public class SystemdictionarytypeController {
    @Autowired
    private ISystemdictionarytypeService systemdictionarytypeService;

    //新增或修改
    @ApiOperation(value = "数据字典添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarytype systemdictionarytype){
        try {
            if (systemdictionarytype.getId()==null){
                systemdictionarytypeService.add(systemdictionarytype);
            }else {
                systemdictionarytypeService.upadate(systemdictionarytype);
            }
             return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙啊，请稍后");
        }
    }
    /**
     * 写接口三要素
     * 1.请求地址是什么
     * 2.请求的参数
     * 3.返回值是什么
     * @param id
     * @return
     */
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult removeId(@PathVariable("id") Long id){
        try {
            systemdictionarytypeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchTree(@RequestBody List<Long> ids){
        try {
            systemdictionarytypeService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //查询数据字典一条数据
    @GetMapping("/{id}")
    public Systemdictionarytype getById(@PathVariable("id") Long id){
        return systemdictionarytypeService.findById(id);
    }
    //查询全部数据
    @GetMapping("/list")
    public List<Systemdictionarytype> getAll(){
        return systemdictionarytypeService.findAll();
    }

    //分页+高级查询
    @PostMapping("/page")
    public PageList<Systemdictionarytype> queryPage(@RequestBody SystemdictionarytypeQuery query){
        return systemdictionarytypeService.queryPage(query);
    }

}
